<?php
/**
 *
 * MongoDBIndexes 索引管理
 *
 * MongoDBIndexes.php
 * Created by IntelliJ IDEA.
 *
 * @author: hjh <hjh@jf.com>
 *
 * Date: 2017/9/27
 * Time: 09:35
 */

use Common\MongoDbBase;

require dirname(__DIR__) . '/cronBase.php';

global $argv;
class MongoDBIndexes extends cronBase
{
    private function usage($app)
    {
        fwrite(STDERR, "Usage:\n   {$app} [GetIndexes|RemoveIndexes] DbName CollectionName [IndexName]\n");
    }

    public function doDefault()
    {
        global $argv;
        $this->usage($argv[0]);
    }

    public function doGetIndexes()
    {
        global $argv;
        if (count($argv) < 4) {
            $this->usage($argv[0]);
            return;
        }
        $dbName = $argv[2];
        $collectionName = $argv[3];

        $mongoBase = new MongoDbBase($dbName, $collectionName);
        foreach ($mongoBase->getIndexes() as $index) {
            var_dump($index);
        }
    }

    public function doRemoveIndexes()
    {
        global $argv;
        if (count($argv) < 4) {
            $this->usage($argv[0]);
            return;
        }
        $dbName = $argv[2];
        $collectionName = $argv[3];

        $indexName = null;
        if (count($argv) > 4) {
            $indexName = $argv[4];
        }
        $mongoBase = new MongoDbBase($dbName, $collectionName);
        $mongoBase->removeIndexes($indexName);
    }
}

$method = null;
if (count($argv) > 1) {
    $method = $argv[1];
}
$app->run($method);
